package cn.iocoder.yudao.module.cook.controller.admin.caidan;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cook.controller.admin.caidan.vo.*;
import cn.iocoder.yudao.module.cook.dal.dataobject.caidan.CaidanDO;
import cn.iocoder.yudao.module.cook.service.caidan.CaidanService;

@Tag(name = "管理后台 - 食谱菜单")
@RestController
@RequestMapping("/cook/caidan")
@Validated
public class CaidanController {

    @Resource
    private CaidanService caidanService;

    @PostMapping("/create")
    @Operation(summary = "创建食谱菜单")
    @PreAuthorize("@ss.hasPermission('cook:caidan:create')")
    public CommonResult<Long> createCaidan(@Valid @RequestBody CaidanSaveReqVO createReqVO) {
        return success(caidanService.createCaidan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新食谱菜单")
    @PreAuthorize("@ss.hasPermission('cook:caidan:update')")
    public CommonResult<Boolean> updateCaidan(@Valid @RequestBody CaidanSaveReqVO updateReqVO) {
        caidanService.updateCaidan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除食谱菜单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cook:caidan:delete')")
    public CommonResult<Boolean> deleteCaidan(@RequestParam("id") Long id) {
        caidanService.deleteCaidan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得食谱菜单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cook:caidan:query')")
    public CommonResult<CaidanRespVO> getCaidan(@RequestParam("id") Long id) {
        CaidanDO caidan = caidanService.getCaidan(id);
        return success(BeanUtils.toBean(caidan, CaidanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得食谱菜单分页")
    @PreAuthorize("@ss.hasPermission('cook:caidan:query')")
    public CommonResult<PageResult<CaidanRespVO>> getCaidanPage(@Valid CaidanPageReqVO pageReqVO) {
        PageResult<CaidanDO> pageResult = caidanService.getCaidanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CaidanRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得食谱菜单列表")
    @PreAuthorize("@ss.hasPermission('cook:caidan:query')")
    public CommonResult<List<CaidanRespVO>> getCaidanList(@Valid CaidanListReqVO listReqVO) {
        List<CaidanDO> list = caidanService.getCaidanList(listReqVO);
        return success(BeanUtils.toBean(list, CaidanRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得食谱菜单列表")
    @PreAuthorize("@ss.hasPermission('cook:caidan:query')")
    public CommonResult<List<CaidanSimpleRespVO>> getCaidanSimpleList() {
        List<CaidanDO> list = caidanService.getCaidanList(
                new CaidanListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
        return success(BeanUtils.toBean(list, CaidanSimpleRespVO.class));

    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出食谱菜单 Excel")
    @PreAuthorize("@ss.hasPermission('cook:caidan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCaidanExcel(@Valid CaidanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CaidanDO> list = caidanService.getCaidanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "食谱菜单.xls", "数据", CaidanRespVO.class,
                        BeanUtils.toBean(list, CaidanRespVO.class));
    }

}